

package com.ylbh.modules.app.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.exception.RRException;
import com.ylbh.common.pojo.home.vo.HomeListVO;
import com.ylbh.common.pojo.home.vo.HomeStateStatisticsVO;
import com.ylbh.common.utils.ShiroUtils;
import com.ylbh.common.validator.Assert;
import com.ylbh.modules.app.service.UserService;
import com.ylbh.modules.app.dao.UserDao;
import com.ylbh.modules.app.entity.UserEntity;
import com.ylbh.modules.app.form.LoginForm;
import com.ylbh.modules.returngoods.service.ShoppingOrderReturnService;
import com.ylbh.modules.shopping_orderform.service.ShoppingOrderformService;
import com.ylbh.modules.sys.entity.SysUserEntity;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

	@Autowired
	private UserDao userDao;

	@Autowired
	private ShoppingOrderformService shoppingOrderformService;

	@Autowired
	private ShoppingOrderReturnService shoppingOrderReturnService;

	@Override
	public UserEntity queryByMobile(String mobile) {
		return baseMapper.selectOne(new QueryWrapper<UserEntity>().eq("mobile", mobile));
	}

	@Override
	public long login(LoginForm form) {
		UserEntity user = queryByMobile(form.getMobile());
		Assert.isNull(user, "手机号或密码错误");

		//密码错误
		if(!user.getPassword().equals(DigestUtils.sha256Hex(form.getPassword()))){
			throw new RRException("手机号或密码错误");
		}

		return user.getUserId();
	}

	@Override
	public HomeListVO queryWeeklyStatistics() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		// 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		// System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
		// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		// 获得当前日期是一个星期的第几天
		int day = cal.get(Calendar.DAY_OF_WEEK);
		// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
		String impTimeBegin = sdf.format(cal.getTime());
		ArrayList<String> list = new ArrayList<>();
		list.add(impTimeBegin);
		for (int i = 0; i < 6; i++) {
			cal.add(Calendar.DAY_OF_MONTH, 1);
			list.add(sdf.format(cal.getTime()));
		}
		List<Integer> count = null;
		if (list.size() >= 7){
			count = userDao.selectMonthCount(list.get(0), list.get(1), list.get(2), list.get(3), list.get(4), list.get(5), list.get(6));

		}
		HomeListVO vo = new HomeListVO();
		vo.setList(count);
		// 待发货订单、待审核联盟店商品、待审核联盟店、待审核提现
		List<HomeStateStatisticsVO> statistics = userDao.selectHomeStateStatistics();
		// 今日订单总数
		Date date = new Date();
//		int orderToday = userDao.selectOrderToday(sdf.format(date));
		//
//		vo.setStatus(statistics);
		// 当前时间的前七天
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date);
		cal1.add(Calendar.DAY_OF_MONTH, -7);
		date = cal1.getTime();
		// 当前时间的前一天
		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date);
		// 今日订单总数、会员总数、今日销售总额、昨日销售总额、近七天销售总额、全部商品、福利商品、兑换商品
		List<Object> num = userDao.selectStatisticsNum(new Date(), cal2.getTime(), date);
		vo.setNum(num);

		DecimalFormat df = new DecimalFormat("0.0");

		// 首页app记录开始
		String format = sdf.format(new Date());
		// 根据今天的时间来查询出今天登录过的用户量
		Double visitor = userDao.selectVisitor(format + " 00:00:00", format + " 23:59:59");
		// 总用户量
		Double sum = userDao.queryCount();
		String visitor1 = df.format((visitor / sum) * 100);
		vo.setVisitor(visitor1);
		// 首页app记录结束

		// 未发货的订单数量开始
		Double unShipped = userDao.selectUnshipped();
		Double unShipped1 = userDao.selectUnshippeds();
		String unShipped2 = df.format((unShipped / unShipped1) * 100);
		vo.setUnShipped(unShipped2);
		// 未发货的订单数量结束

		// 注册未登录开始
		Double notLogin = userDao.selectNotLogin();
		String notLogin1 = df.format((notLogin / sum) * 100);
		vo.setNotLogin(notLogin1);
		// 注册未登录结束
		SysUserEntity entity = userDao.selectUserName(ShiroUtils.getUserId());
		vo.setUser(entity);

		// 退换货未审核订单开始
		List<HomeStateStatisticsVO> orderReturnCount = shoppingOrderReturnService.queryOrderReturn();
		for (HomeStateStatisticsVO v : orderReturnCount) {
			statistics.add(v);
		}
		vo.setStatus(statistics);
		// 退换货未审核订单结束
		return vo;
	}

}
